package cn.wx.pet.service.impl;

import cn.wx.basic.exception.BusinessException;
import cn.wx.basic.service.IBaiduAiAuditService;
import cn.wx.basic.service.impl.BaseServiceImpl;
import cn.wx.basic.util.CodeGenerateUtils;
import cn.wx.basic.util.DistanceUtils;
import cn.wx.basic.util.Point;
import cn.wx.order.mapper.OrderPetAcquisitionMapper;
import cn.wx.org.domain.Shop;
import cn.wx.org.mapper.ShopMapper;
import cn.wx.order.domain.OrderPetAcquisition;
import cn.wx.pet.domain.Pet;
import cn.wx.pet.domain.SearchMasterMsg;
import cn.wx.pet.domain.SearchMasterMsgAuditLog;
import cn.wx.pet.dto.AcceptDto;
import cn.wx.pet.mapper.PetMapper;
import cn.wx.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.wx.pet.mapper.SearchMasterMsgMapper;
import cn.wx.pet.service.ISearchMasterMsgService;
import cn.wx.user.domain.User;
import cn.wx.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private IBaiduAiAuditService iBaiduAiAuditService;

    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private PetMapper petMapper;

    /*
    *     寻主
    * */
    @Override
    public void publish(SearchMasterMsg ssm, Long id) {
        //给user_id 和 shop_id 赋值
        User user =  userMapper.findByLogininfoId(id);

        //通过传回来的地址计算出经纬度
        Point point = DistanceUtils.getPoint(ssm.getAddress());

        //查询所有店铺
        List<Shop> shops = shopMapper.loadAll();
        Shop shop = DistanceUtils.getNearestShop(point, shops);

        ssm.setUser_id(user.getId());
        ssm.setShop_id(shop.getId());
        searchMasterMsgMapper.save(ssm);


        //审核
        //百度ai校验文本信息
        Boolean textBoolean = iBaiduAiAuditService.textAudit(ssm.getName() + ssm.getAddress() + ssm.getCoatColor());
        //百度ai校验图片信息
        /*Boolean ImgBoolean = iBaiduAiAuditService.imageAudit(ssm.getResources());*/

        if(textBoolean){
            //审核通过
            //修改状态
            ssm.setState(1);  //待处理
            ssm.setNote("待处理！！");
            searchMasterMsgMapper.update(ssm);


            //存入t_search_master_msg_audit_log日志表
            SearchMasterMsgAuditLog smmal = new SearchMasterMsgAuditLog();
            smmal.setMsg_id(ssm.getId());
            smmal.setAudit_time(new Date());
            smmal.setState(1);
            smmal.setNote("审核待处理！！");
            searchMasterMsgAuditLogMapper.save(smmal);
        }else {  //审核未通过
            ssm.setNote("驳回了！！");
            searchMasterMsgMapper.update(ssm);


            //存入t_search_master_msg_audit_log日志表
            SearchMasterMsgAuditLog smmal = new SearchMasterMsgAuditLog();
            smmal.setMsg_id(ssm.getId());
            smmal.setAudit_time(new Date());
            smmal.setState(0);
            smmal.setNote("审核不通过，驳回！！");
            searchMasterMsgAuditLogMapper.save(smmal);
        }
    }

    @Override
    public void reject(Long id) {
        searchMasterMsgMapper.reject(id);
    }

    @Override
    public void accept(AcceptDto dto) {
        //代理人 接单的员工id
        Long handler = dto.getHandler();
        //寻主的表的id
        Long msgId = dto.getMsgId();
        //备注
        String note = dto.getNote();

        // 根据smmid查询订单，如果查的到，该订单正在处理中，
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadBySmmId(msgId);
        if(order!=null){ // 之前接单了
            throw  new BusinessException("订单正在处理中。。。！");
        }
        //  1. 生成一个pet : 根据searchMasterMsg生成。
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(msgId);

        Pet pet = searchMasterMsg2Pet(searchMasterMsg);
        // 保存,保存id
        petMapper.save(pet);

        // 2. 生成一个收购订单：
        OrderPetAcquisition opa = ininOrderPetAcquisition(dto,pet,searchMasterMsg);

        //保存订单表
        orderPetAcquisitionMapper.save(opa);


    }
    /**
     * 生成一个订单对象
     * @param dto
     * @param pet
     * @return
     */
    private OrderPetAcquisition ininOrderPetAcquisition(AcceptDto dto, Pet pet, SearchMasterMsg ssm) {
        // 创建一个订单对象，赋值
        OrderPetAcquisition opa = new OrderPetAcquisition();

        // 摘要
        opa.setDigest("【"+ssm.getTitle()+"】");

        //创建订单编号
        String s = CodeGenerateUtils.generateOrderSn(ssm.getUser_id());
        opa.setOrderSn(s);
        //地址
        opa.setAddress(ssm.getAddress());
        //创建时间
        opa.setLastcomfirmtime( new Date(System.currentTimeMillis()+2*24*60*60*1000));
        //处理人
        opa.setEmp_id(dto.getHandler());
        //用户订单id
        opa.setSearch_master_msg_id(dto.getMsgId());
        //宠物id
        opa.setPet_id(pet.getId());
        //店铺id
        opa.setShop_id(ssm.getShop_id());
        //登录人
        opa.setUser_id(ssm.getUser_id());
        // 待处理  因为还没没有买回来
        opa.setState(0);
        //价格
        opa.setPrice(ssm.getPrice());

        return opa;
    }

    /**
     * 通过searchMasterMsg 生成一个pet
     * @param smm
     * @return
     */
    private Pet searchMasterMsg2Pet(SearchMasterMsg smm) {
        Pet pet = new Pet();
        pet.setName(smm.getName());
        pet.setCostprice(smm.getPrice());
        pet.setSearch_master_msg_id(smm.getId());
        pet.setShop_id(smm.getShop_id());
        pet.setType_id(smm.getPetType());
        pet.setResources(smm.getResources());
        return  pet;


    }


}
